<!-- 设置公务号码所属分类 -->
<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="modalTitle"
    @ok="onButtonOkClick"
    :body-style="bodyStyle"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, unref, inject } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';

  import { setCategoryFormSchema } from './officialphone.data';
  import { setCategory } from '/@/api/addressbook/official/phone';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { OperationTypeEnum } from '/@/enums/appEnum';

  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'SetCategoryForPhones',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      //弹出窗口界面是新增数据、修改数据；根据传递过来的参数进行判断
      const op = ref(OperationTypeEnum.UN_KNOWN);

      const dataIds = ref([]);

      const moduleName = inject<string>('moduleName');
      //标题--根据传入的模块名称+操作类型组合得到
      const modalTitle = ref('标题');

      const bodyStyle = {
        height: '350px',
        // overflow: 'hidden',
        // overflowY: 'scroll',
      };

      const [registerForm, { resetFields, validate }] = useForm({
        labelWidth: 80,
        schemas: setCategoryFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      /**
       * 根据父组件传递过来的参数获取对应的操作，操作不同所对应的界面不同
       */
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        console.log(data);
        //接收父组件传递的数据
        if (data?.op) {
          //判断是否传入op属性
          op.value = data.op;
        }

        if (data?.phoneIds) {
          dataIds.value = data.phoneIds;
          console.log('选中的号码', dataIds);
        }

        if (moduleName) {
          modalTitle.value = unref(moduleName);
        }
        if (op.value != OperationTypeEnum.UN_KNOWN && op.value != OperationTypeEnum.CUSTOME) {
          modalTitle.value += '--' + op.value;
        }

        resetFields(); //重置表单
        setModalProps({ confirmLoading: false });
      });

      /**
       * 点击确认按钮，根据不同的op调用对应方法保存信息
       */
      async function onButtonOkClick() {
        try {
          const values = await validate();
          console.log('values', values.categoryId);
          setModalProps({ confirmLoading: true });
          //调用接口进行保存
          setCategory({ ids: dataIds.value, categoryId: values.categoryId })
            .then((res) => {
              if (!res || Object.keys(res).length == 0) {
                createMessage.success('设置成功。', 2);
              }
              opSuccess();
            })
            .catch((ex) => {
              console.log('catch ', ex);
              createMessage.error(ex.msg);
            });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      //操作成功后
      function opSuccess() {
        closeModal();
        emit('success');
      }

      return {
        op,
        moduleName,
        modalTitle,
        registerModal,
        registerForm,
        onButtonOkClick,
        opSuccess,
        bodyStyle,
      };
    },
  });
</script>
